package com.qingke.oms.command.user;

import java.util.List;

import com.qingke.oms.command.UserCommand;
import com.qingke.oms.command.shop.UpdateGoodsStatistic;
import com.qingke.oms.pojo.Order;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class PayCommand extends UserCommand implements UpdateGoodsStatistic{

	public PayCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		List<Order> list=dao.selectOrderByUserIdAndStatus(user.getId(),"wait_pay");
		if(list.size()==0){
			OMSConsole.println("you have no order need to pay.");
			return ;
		}
		String cmd;
		while(true){
			cmd=OMSConsole.prompt("pay>");
			if("all".equalsIgnoreCase(cmd)){
				all(list);
			}
			else if("id".equalsIgnoreCase(cmd)){
				id(list);
			}
			else if("help".equalsIgnoreCase(cmd)){
				help();
			}
			else if("exit".equalsIgnoreCase(cmd)){
				break;
			}
			else{
				OMSConsole.println("err input");
				continue;
			}
		}
	}
	
	private void all(List<Order> list){
		double total=0;
		for(Order order:list){
			total+=order.getTotal();
		}
		if(total>user.getBalance()){
			OMSConsole.println("you don't have enough money to pay all orders.");
			return ;
		}
		
		int count=0;
		for(Order order:list){
			boolean isSuccess=dao.insertTransactionStatusRecord(order.getId(), "wait_delivery");
			
			if(isSuccess)
				isSuccess=dao.insertShipmentRecord(order, "not shipped");
			else
				continue;
			
			if(isSuccess)
				isSuccess=dao.updateUserBalanceToShopTurnover(user.getId(),order.getShopId(), order.getTotal());
			else
				continue;
			
			if(isSuccess){
				updateGoodsStatistic();
				count++;
			}
		}
		if(count!=list.size()){
			OMSConsole.println("pay all orders failed.");
			return ;
		}
		OMSConsole.println("pay all orders success.");
 	}
	
	private void id(List<Order> list){
		int orderId=OMSConsole.getInt("order id:");
		Order order=dao.selectOrderById(orderId);
		if(order==null){
			OMSConsole.println("The id of the order is not existed.");
			return ;
		}
		if(!list.contains(order)){
			OMSConsole.println("The id of the order is not yours.");
			return ;
		}
		if(order.getTotal()>user.getBalance()){
			OMSConsole.println("you don't have enough money to pay the order.");
			return ;
		}
		boolean isSuccess=dao.insertTransactionStatusRecord(order.getId(), "wait_delivery");
		
		if(isSuccess){
			isSuccess=dao.insertShipmentRecord(order, "not shipped");
		}
		else{
			OMSConsole.println("create transaction status record failed.");
			return ;
		}
		
		if(isSuccess){
			isSuccess=dao.updateUserBalanceToShopTurnover(user.getId(),order.getShopId(), order.getTotal());
		}
		else{
			OMSConsole.println("create shipment record failed.");
			return ;
		}
		
		if(isSuccess){
			OMSConsole.println("pay the order success.");
			updateGoodsStatistic();
		}
		else {
			OMSConsole.println("user balance to shop turnover failed.");
			return ;
		}
		
	}
	
	private void help(){
		OMSConsole.println("- all    pay all orders");
		OMSConsole.println("- id     pay the id of the order");
		OMSConsole.println("- exit   exit pay command");
	}
	

}
